<%@page import="java.util.StringTokenizer"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="Configurations.MySQL"%>
<%
    session.setAttribute("displayPage", null);
    // Redirect if this page is being displayed by itself
    if (request.getServletPath().equals("/home.jsp")) {
        System.out.println("Current Page is set to home. Redirect to index.jsp");
        response.sendRedirect("");
    }
    // Redirect to single application page if user has made a selection.
    if (request.getParameter("applicationEmailHome") != null) {
        session.setAttribute("displayPage", "application");
        // Store requested application email
        session.setAttribute("appRequest", request.getParameter("applicationEmailHome"));
        response.sendRedirect("");
    }
%>
<h3>Currently Assigned Applications:</h3>
<%
    // Store the current username
    String username = (String) session.getAttribute("username");
    // Establish a connection to the MySQL database
    Connection con = null;
    ResultSet rs = null;
    PreparedStatement ps = null;
    try {
        Class.forName(MySQL.DB_DRIVER);
        con = DriverManager.getConnection(MySQL.DB_URL, MySQL.DB_USER, MySQL.DB_PASS);
        // Get information about the current user
        String sql = "SELECT * FROM " + MySQL.DB_LOGIN + " WHERE " + MySQL.DB_LOGIN_USERNAME + "=?";
        ps = con.prepareStatement(sql);
        ps.setString(1, username);
        rs = ps.executeQuery();
        if (rs.next()) { // A match is found (Expected to find one)
            String str = rs.getString("assigned"); // Retrieve string of application emails
            StringTokenizer tokenizer = new StringTokenizer(str, ",");
            System.out.println("Has applications to review?" + tokenizer.hasMoreTokens());
            if (!tokenizer.hasMoreTokens()) {
                out.println("No applications to review");
            } else {
%>
<form action="" method="post">
    <table border="1">
        <tr align="center">
            <td width="5%"></td>
            <td width="20%"><b>Email</b></td>
            <td width="20%"><b>Last Name</b></td>
            <td width="20%"><b>First Name</b></td>
        </tr> 
        <%  while (tokenizer.hasMoreTokens()) {
                sql = "SELECT * FROM " + MySQL.DB_APPS + " WHERE " + MySQL.DB_APPS_EMAIL + "=?";
                ps = con.prepareStatement(sql);
                ps.setString(1, tokenizer.nextToken());
                rs = ps.executeQuery();
                rs.next();
        %>
        <tr align="center">
            <td><input type="radio" name="applicationEmailHome" value=<%=rs.getString(MySQL.DB_APPS_EMAIL)%> checked/></td>
            <td><%=rs.getString(MySQL.DB_APPS_EMAIL)%></td>
            <td><%=rs.getString(MySQL.DB_APPS_LAST)%></td>
            <td><%=rs.getString(MySQL.DB_APPS_FIRST)%></td>
        </tr>
        <%
            }
        %>
    </table>
    <br/>
    <input type="submit" value="View Selected Application"/>
</form>
<%
            }
            System.out.println("Application List: " + str);
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Query for user information: Failure");
    } finally {
        con.close();
        rs.close();
        ps.close();
    }
%>